const init={
    CH : 320,
    CW: 300,
    OffsetX : 30,
    OffsetY : 30,
    Radius: 30,
}
const PointLocationArr=[];
const CaculateNinePointLotion=(diffX, diffY)=>{

    console.log(diffX,diffY)

    for (let row = 0; row < 3; row++) {
        for (let col = 0; col < 3; col++) {
            const Point = {
                X: (init.OffsetX + col * diffX + ( col * 2 + 1) * init.Radius),
                Y: (init.OffsetY + row * diffY + (row * 2 + 1) * init.Radius)
            };
            PointLocationArr.push(Point);
        }
    }
};
const Draw=(cxt, LinePointArr,touchPoint,top=0)=>{
    if (LinePointArr.length > 0) {
        cxt.beginPath();
        for (const value of LinePointArr) {
            const pointIndex = value;
            cxt.lineTo(PointLocationArr[pointIndex].X, PointLocationArr[pointIndex].Y);
        }
        cxt.lineWidth = 10;
        cxt.strokeStyle = "#ffffff";
        cxt.stroke();
        cxt.closePath();
        if(touchPoint!=null){
            const lastPointIndex=LinePointArr[LinePointArr.length-1];
            const lastPoint=PointLocationArr[lastPointIndex];
            cxt.beginPath();
            cxt.moveTo(lastPoint.X,lastPoint.Y);
            cxt.lineTo(touchPoint.X,touchPoint.Y-top);
            cxt.stroke();
            cxt.closePath();
        }
    }
    for (let i = 0; i < PointLocationArr.length; i++) {
        const Point = PointLocationArr[i];
        cxt.fillStyle = "#ffffff";
        cxt.beginPath();
        cxt.arc(Point.X, Point.Y, init.Radius, 0, Math.PI * 2, true);
        cxt.closePath();
        cxt.fill();
        cxt.fillStyle = "#3d84d3";
        cxt.beginPath();
        cxt.arc(Point.X, Point.Y, init.Radius - 3, 0, Math.PI * 2, true);
        cxt.closePath();
        cxt.fill();
        if(LinePointArr.indexOf(i)>=0){
            cxt.fillStyle = "#ffffff";
            cxt.beginPath();
            cxt.arc(Point.X, Point.Y, init.Radius -16, 0, Math.PI * 2, true);
            cxt.closePath();
            cxt.fill();
        }
    }
}
const IsPointSelect=(touches,LinePoint,top)=>{
    for (let i = 0; i < PointLocationArr.length; i++) {
        const currentPoint = PointLocationArr[i];
        const xdiff = Math.abs(currentPoint.X - touches.pageX);
        const ydiff = Math.abs(currentPoint.Y+top - touches.pageY);
        const dir = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5);
        if (dir < init.Radius*1.4 ) {
            if(LinePoint.indexOf(i) < 0){ LinePoint.push(i);}
            break;
        }
    }
}

export {
    init,
    PointLocationArr,
    CaculateNinePointLotion,
    Draw,
    IsPointSelect
}